package com.eascs.admin.o2o.loan.notify.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.admin.o2o.internal.entity.LoanapplyExpand;
import com.eascs.admin.o2o.internal.entity.LoanapplyExpandDetail;
import com.eascs.admin.o2o.internal.service.LoanapplyExpandDetailService;
import com.eascs.admin.o2o.internal.service.LoanapplyExpandService;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.notify.O2OMsgNotifyService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.workflow.common.entity.wf.ProcessTaskMain;
import com.eascs.workflow.common.entity.wf.ProcessTemplate;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.common.vo.WFNotifyParam;

/**
 * 收保证金管理费流程
 */
@Service
public class BZJMsgNotifyServiceImpl implements O2OMsgNotifyService {
	private static Logger log = LoggerFactory.getLogger(BZJMsgNotifyServiceImpl.class);
	
	@Autowired
	private LoanapplyExpandDetailService loanapplyExpandDetailService;
	
	@Autowired
	private LoanapplyExpandService loanapplyExpandService;

	@Override
	public ModelResult<O2oLoanapplyEntity> notify(WFNotifyParam notifyParam, O2oLoanapplyEntity loanapply){
		ModelResult<O2oLoanapplyEntity> modelResult =new ModelResult<>();
		log.info("启动归档流程通知:[{}]", StatusContexts.WFTYPE_BZJ);
		loanapply = bzjFlow(notifyParam, loanapply);
		/*if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_PASS) {
			log.info("归档流程通过记录通知：{}", loanapply.getBillCode());
			loanapply.setIsPlaceOnFile(StatusContexts.WFTYPE_STATUS_CLO);
			loanapply.setPlaceOnFileTime(new Date());
		} else if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_TURNED_DOWN) {
			log.info("归档流程驳回记录通知：{}", loanapply.getBillCode());
			loanapply.setIsPlaceOnFile(StatusContexts.O2O_NEW);
		} else if (notifyParam.getProcessStatus().equals(ProcessTaskMain.PROECESS_STATUS_REJECT)) {
			log.info("归档流程作废记录通知：{}", loanapply.getBillCode());
		}*/
		modelResult.withModel(loanapply);
		return modelResult;
	}

	@Override
	public List<String> getServiceName() {
		ArrayList<String> list = new ArrayList<String>();
		list.add(ProcessTemplate.PROECESS_TYPE_DEPOSIT);
		list.add(ProcessTemplate.PROECESS_TYPE_DEPOSIT_FQ);
		return list;
	}
	
	@Override
	public Boolean checkReturn() {
		return Boolean.FALSE;
	}
	
	/**
	 * 保证金流程
	 * 
	 * @param notifyParam
	 * @param loanapplyEntity
	 */
	private O2oLoanapplyEntity bzjFlow(WFNotifyParam notifyParam, O2oLoanapplyEntity loanapplyEntity) {
		LoanapplyExpand loanapplyExpand = loanapplyExpandService.queryLoanapplyNo(loanapplyEntity.getLoanapplyNo());
		if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_PASS) {// 流程通过
			loanapplyExpand.setBillStatus(StatusContexts.BZJ_STATUS_CLO);
			loanapplyExpand.setLoanapply_no(notifyParam.getReferId());
			int i = loanapplyExpandService.updateWorkFlow(loanapplyExpand);
			if (i <= 0) {
				log.info("贷款编号:[{}],保证金流程完成,修改失败", loanapplyEntity.getBillCode());
				throw new RuntimeException();
			}
			log.info("贷款编号:[{}],保证金流程完成,修改状态为：[{}]", loanapplyEntity.getBillCode(), StatusContexts.BZJ_STATUS_CLO);
		} else if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_TURNED_DOWN) {// 流程驳回
			loanapplyExpand.setBillStatus(StatusContexts.BZJ_STATUS_NEW);
			loanapplyExpand.setLoanapply_no(notifyParam.getReferId());
			int a = loanapplyExpandService.updateWorkFlow(loanapplyExpand);
			if (a <= 0) {
				log.info("贷款编号:[{}],保证金流程驳回,修改LoanapplyExpand表失败", loanapplyEntity.getBillCode());
				throw new RuntimeException();
			}
			log.info("贷款编号:[{}],保证金流程驳回,修改LoanapplyExpand表状态为：[{}]", loanapplyEntity.getBillCode(), StatusContexts.BZJ_STATUS_CLO);
		} else if (notifyParam.getProcessStatus().equals(ProcessTaskMain.PROECESS_STATUS_REJECT)) {// 作废
			log.info("贷款编号:[{}],内审流程作废,修改为主表状态为：[{}]", loanapplyEntity.getBillCode(), StatusContexts.O2O_FK_OVER);
			loanapplyEntity.setWfType(StatusContexts.O2O_FK_OVER);
		}
		/******************* 通过或者驳回,删除保证金中间表数据 ***********************/
		LoanapplyExpandDetail loanapplyExpandDetail = loanapplyExpandDetailService.queryDetail(loanapplyExpand.getId());
		if (loanapplyExpandDetail != null) {
			loanapplyExpandDetailService.delete(loanapplyExpandDetail.getId());
			log.info("保证金通过删除中间表数据,贷款单号：[{}],id[{}]", loanapplyExpandDetail.getBillCode(), loanapplyExpandDetail.getId());
		}
		return loanapplyEntity;
	}
	
	
}
